/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jcoyote;

import java.io.*;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import java.util.Scanner;

/**
 *
 * @author coyote
 */
public class Init {
    
    public   String version  = "0.0.19";
    private  String address  = "" ;
    private  String rcon     = "" ;
    private  String pathlog  = "" ;
    private  String port     = "" ;
    
    public Init() throws UnknownHostException, MalformedURLException, IOException, InterruptedException {
        
       print_ASCIIart();
       System.out.println("CoyoteBoT © version " +version +"\n"); 
       System.out.println("Pour la team MQCD, et la communautée Urban Terror France");
       System.out.println("Par Asloon, protégé sous licence GNU/GPL\n");
       
       File cfgconfig = new File("config.ini");
       
       if(!cfgconfig.exists()) { // If this config file doesn't exist, we create it.
            System.out.print("Assistant de creation de fichier de configuration :");

            System.out.print("\n\nAdresse IP du serveur UrT [localhost] : ");
            Scanner sc = new Scanner(System.in);
            address = sc.nextLine();
            if (address.equals("") || address.equals("localhost"))
                address = "127.0.0.1";
            

            System.out.print("\nPort du serveur UrT [27960] : ");
            port = sc.nextLine();
            if(port.equals("")) 
                port = "27960" ;
            
            System.out.print("\nMot de passe RCon : ");
            rcon = sc.nextLine();

            System.out.print("\nChemin absolu du games.log : ");
            pathlog = sc.nextLine();

            FileWriter fw = new FileWriter("config.ini");
            BufferedWriter output = new BufferedWriter(fw);

            output.write("// Fichier de configuration du CoyoteBoT \n\n");
            output.write("// Adresse IP du serveur UrbanTerror \n");
            output.write("{IP} : " +address +"\n\n");
            output.write("// Port du serveur  \n");
            output.write("{Port} : " +port +" \n\n");
            output.write("// Mot de passe Rcon\n");
            output.write("{RCon} : " +rcon +" \n\n");
            output.write("// Chemin du fichier de log du serveur UrT (games.log) \n");
            output.write("{LoGPath} : " +pathlog +"\n");
            output.flush();
            output.close();
            fw.close();
            
            System.out.println("\n Configuration Terminée.\n");
            Thread.sleep(2);
       }
       else {
           System.out.print("Lecture du fichier de configuration...");
           
           RandomAccessFile raf = new RandomAccessFile(cfgconfig.getAbsolutePath(), "r");
           String ln ;
           String[] split ;
           
           while ((ln = raf.readLine()) != null ) {
               
               if(ln.contains("{IP}")) {
                   split = ln.split(":");
                   address = split[1].replaceAll(" ", "");
               }
               
               if(ln.contains("{Port}")) {
                   split = ln.split(":");
                   port = split[1].replaceAll(" ", "");
               }
               
               if(ln.contains("{RCon}")) {
                   split = ln.split(":");
                   rcon = split[1].replaceAll(" ", "");
               }
               
               if(ln.contains("{LoGPath}")) {
                   split = ln.split(":");
                   pathlog = split[1].replaceAll(" ", "");
               }
           }
           raf.close();
           System.out.println("[OK]\n");
           System.out.println("-------------------------------");
           System.out.println("  IP : " +address);
           System.out.println("  Port : " +port);
           System.out.println("  Rcon : " +rcon);
           System.out.println("  LogPath : "+pathlog);
           System.out.println("-------------------------------\n");
       }
       
    }

   private void print_ASCIIart() throws InterruptedException {

       String AsciiArt = "          ____                  _       ____      _____               \n         / _ _|___  _   _"
                       + "  ___ | |_ ___| __ )  __|_   _|               \n        | |   / _ \\| | | |/ _ \\| __/ _ \\  _ \\"
                       + " / _ \\| |            \n        | |__| (_) | |_| | (_) | ||  __/ |_) | (_) | |                 \n"
                       + "         \\____\\___/ \\__, |\\___/ \\__\\___|____/ \\___/|_|          \n                    |___"
                       + "/                                              \n\n\n                                            ";
       char[] AAarray = AsciiArt.toCharArray();
       for(int i = 0 ; i < 421 ; i++){
           System.out.print(AAarray[i]); 
           Thread.sleep(0x1); // 0x5 in public mode
       }
          
    }
   
   public String getServerIP() {
       return address ;
   }
   
   public String getServerPort() {
       return port ;
   }
   
   public String getServerRCon() {
       return rcon ;
   }
    
   public String getServerLoGPath() {
       return pathlog ;
   }
}
